#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>

#define MAX_STUDENT 100

struct Student {
    char gender;
    int id;
    char *name;
    uint8_t age;
    float c_score;
    float english_score;
    float database_score;
    float total_score;
} student_array[MAX_STUDENT];

int student_count = 0;

void menu();
void add_student();
void query_student();
void modify_student();
void delete_student();
void calculate_score();
void sort_student();
void print_one_student();
void print_all_student();
void exit_program();

int main(int argc, char* argv[]) {
    printf("%d\r\n", argc);
    printf("%s\r\n", *argv);

    menu();

    return 0;
}

void menu() {
    printf("1、录入信息\r\n"
           "2、查询信息\r\n"
           "3、修改信息\r\n"
           "4、删除信息\r\n"
           "5、计算总分\r\n"
           "6、排序信息\r\n"
           "7、输出全部\r\n"
           "8、退出程序\r\n");

    int menu = 0;
    scanf("%d", &menu);

    switch (menu) {
    case -1: exit_program(); break;
    case 1: add_student(); break;
    case 2: query_student(); break;
    case 3: modify_student(); break;
    case 4: delete_student(); break;
    case 5: calculate_score(); break;
    case 6: sort_student(); break;
    case 7: print_all_student(); break;
    case 8: exit_program(); break;
    default: printf("未知指令\r\n");
    }
}

void exit_program() {
    exit(-1);
}


void add_student() {
    if (student_count < MAX_STUDENT) {
        student_array[student_count].name = malloc(50);
        printf("请输入学生信息：");
        scanf("%s", student_array[student_count].name);
        printf("请输入年龄：");
        scanf("%c", &student_array[student_count].age);
        printf("请输入编号：");
        scanf("%d", &student_array[student_count].id);
        printf("请输入性别：");
        scanf("%c", &student_array[student_count].gender);
        printf("请输入C语言分数：");
        scanf("%f", &student_array[student_count].c_score);
        printf("请输入英语分数：");
        scanf("%f", &student_array[student_count].english_score);
        printf("请输入数据库分数：");
        scanf("%f", &student_array[student_count].database_score);
        student_array[student_count].total_score = 0;
    } else {
        printf("学生人数已满\r\n");
    }
}

int get_student_index(int id) {
    int result = -1;

    for(int i = 0; i < student_count; i++) {
        if (student_array[i].id == id) {
            result = i;
            break;
        }
    }

    return result;
}

void print_one_student(struct Student student) {
    printf("姓名：%s，编号：%d，年龄：%d，性别：%c,C语言分数：%f，英语分数：%f，数据库分数：%f", student.name, student.id, student.age, student.gender, student.c_score, student.english_score, student.database_score);
}

void print_all_student() {
    for(int i = 0; i < student_count; i++) {
        print_one_student(student_array[i]);
    }
}

void query_student() {
    int result = -1;
    printf("请输入学生编号：");
    int id = 0;
    scanf("%d", &id);
    if (id < 0) {
        printf("非法编号\r\n");
        return;
    }
    result = get_student_index(id);
    if (result > -1) {
        print_one_student(student_array[result]);
    }
}

void modify_one_student(int index) {
    print_one_student(student_array[index]);

    int i = -1;
    while(true) {
        printf("请输入要修改的字段：1/姓名，2/性别，3/年龄，4/C语言分数，5/英语分数，6/数据库分数。");
        scanf("%d", &i);
        if (i >= 1 && i <= 6) {
            break;
        }
    }

    switch(i) {
        case 1: {
            if (student_array[index].name != NULL) {
                free(student_array[index].name);
            }
            student_array[index].name = malloc(50);
            printf("请输入姓名：");
            scanf("%s", student_array[index].name);
        }; break;
        case 2: {
            printf("请输入性别：");
            scanf("%c", &student_array[index].gender);
        }; break;
        case 3: {
            printf("请输入年龄：");
            scanf("%c", &student_array[index].age);
        }; break;
        case 4: {
            printf("请输入C语言分数：");
            scanf("%f", &student_array[index].c_score);
        }; break;
        case 5: {
            printf("请输入英语分数：");
            scanf("%f", &student_array[index].english_score);
        }; break;
        case 6: {
            printf("请输入数据库分数：");
            scanf("%f", &student_array[index].database_score);
        }; break;
    }
    printf("是否继续修改该用户？");
    scanf("%d", &i);
    if (i > 0) {
        modify_one_student(index);
    }
}

void modify_student() {
    int result = -1;
    printf("请输入学生编号：");
    int id = 0;
    scanf("%d", &id);
    if (id < 0) {
        printf("非法编号\r\n");
        return;
    }

    result = get_student_index(id);
    if (result > -1) {
        modify_one_student(result);
        printf("修改成功\r\n");
    }
}

void delete_one_student(int index) {
    free(student_array[index].name);
    for(int i = index; i < student_count; i++) {
        student_array[i] = student_array[i + 1];
    }
    student_count--;
}

void delete_student() {
    int result = -1;
    printf("请输入学生编号：");
    int id = 0;
    scanf("%d", &id);
    if (id < 0) {
        printf("非法编号\r\n");
        return;
    }

    result = get_student_index(id);
    if (result > -1) {
        delete_one_student(result);
        printf("删除成功\r\n");
    }
}

void calculate_score() {
    for(int i = 0; i < student_count; i++) {
        student_array[i].total_score = student_array[i].c_score + student_array[i].english_score + student_array[i].database_score;
        printf("编号为 %d 的同学：%s 总分数为：%f\r\n", student_array[i].id, student_array[i].name, student_array[i].total_score);
    }
}

void sort_student() {
    struct Student tmp;
    for(int i = 0; i < student_count - 1; i++) {
       if(student_array[i].id > student_array[i + 1].id) {
           tmp = student_array[i];
           student_array[i] = student_array[i + 1];
           student_array[i + 1] = tmp;
       }
    }
}

